home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GP_MSDOS.C < prev    next >
C/C++ Source or Header  |  1992-03-21  |  7KB  |  213 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gp_msdos.c */
  21. /* Common platform-specific routines for MS-DOS (any compiler) */
  22. #include <stdio.h>
  23. #include "dos_.h"
  24. #include "string_.h"
  25. #include "gx.h"
  26. #include "gp.h"
  27.  
  28. /* ------ Date and time ------ */
  29.  
  30. /* Read the current date (in days since Jan. 1, 1980) */
  31. /* and time (in milliseconds since midnight). */
  32. void
  33. gp_get_clock(long *pdt)
  34. {    union REGS osdate, ostime;
  35.     long idate;
  36.     static int mstart[12] =
  37.        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  38.     osdate.h.ah = 0x2a;        /* get date */
  39.     intdos(&osdate, &osdate);
  40. #define da_year rshort.cx
  41. #define da_mon h.dh
  42. #define da_day h.dl
  43.     ostime.h.ah = 0x2c;        /* get time */
  44.     intdos(&ostime, &ostime);
  45. #define ti_hour h.ch
  46. #define ti_min h.cl
  47. #define ti_sec h.dh
  48. #define ti_hund h.dl
  49.     idate = (long)osdate.da_year * 365 +
  50.         (osdate.da_year / 4 + 1 +    /* account for leap years */
  51.          mstart[osdate.da_mon - 1] +    /* month is 1-origin */
  52.          osdate.da_day - 1);        /* day of month is 1-origin */
  53.     if ( osdate.da_mon <= 2 && osdate.da_year % 4 == 0 )        /* Jan. or Feb. of leap year */
  54.         idate--;
  55.     pdt[0] = idate;
  56.     pdt[1] =
  57.         (ostime.ti_hour * 60 + ostime.ti_min) * 60000L +
  58.         (ostime.ti_sec * 100 + ostime.ti_hund) * 10L;
  59. }
  60.  
  61. /* ------ Printer accessing ------ */
  62.  
  63. /* Open a connection to a printer.  A null file name means use the */
  64. /* standard printer connected to the machine, if any. */
  65. /* Return NULL if the connection could not be opened. */
  66. FILE *
  67. gp_open_printer(char *fname)
  68. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  69.        {    union REGS regs;
  70.         regs.h.ah = 0x44;    /* ioctl */
  71.         regs.h.al = 0;        /* get device info */
  72.         regs.rshort.bx = fileno(stdprn);
  73.         intdos(®s, ®s);
  74.         regs.h.dl |= 0x20;    /* binary (no ^Z intervention) */
  75.         regs.h.dh = 0;
  76.         regs.h.ah = 0x44;    /* ioctl */
  77.         regs.h.al = 1;        /* set device info */
  78.         intdos(®s, ®s);
  79.         return stdprn;
  80.        }
  81.     else
  82.         return fopen(fname, "wb");
  83. }
  84.  
  85. /* Close the connection to the printer. */
  86. void
  87. gp_close_printer(FILE *pfile, const char *fname)
  88. {    if ( pfile != stdprn )
  89.         fclose(pfile);
  90. }
  91.  
  92. /* ------ File names ------ */
  93.  
  94. /* Define the character used for separating file names in a list. */
  95. const char gp_file_name_list_separator = ';';
  96.  
  97. /* Define the default scratch file name prefix. */
  98. const char gp_scratch_file_name_prefix[] = "_temp_";
  99.  
  100. /* Define whether case is insignificant in file names. */
  101. const int gp_file_names_ignore_case = 1;
  102.  
  103. /* Answer whether a file name contains a directory/device specification, */
  104. /* i.e. is absolute (not directory- or device-relative). */
  105. int
  106. gp_file_name_is_absolute(const char *fname, uint len)
  107. {    /* A file name is absolute if it contains a drive specification */
  108.     /* (second character is a :) or if it start with / or \. */
  109.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  110.         (len >= 2 && fname[1] == ':')) );
  111. }
  112.  
  113. /* Answer the string to be used for combining a directory/device prefix */
  114. /* with a base file name.  The file name is known to not be absolute. */
  115. char *
  116. gp_file_name_concat_string(const char *prefix, uint plen,
  117.   const char *fname, uint len)
  118. {    if ( plen > 0 )
  119.       switch ( prefix[plen - 1] )
  120.        {    case ':': case '/': case '\\': return "";
  121.        };
  122.     return "\\";
  123. }
  124.  
  125. /* ------ File enumeration ------ */
  126.  
  127. struct file_enum_s {
  128.     ff_struct_t ffblk;
  129.     char *pattern;
  130.     int patlen;            /* allocated length of pattern */
  131.     int head_size;            /* pattern length through last */
  132.                     /* : or \ */
  133.     int first_time;
  134.     gs_memory_procs mprocs;
  135. };
  136.  
  137. /* Initialize an enumeration.  Note that * and ? in a directory */
  138. /* don't work, and \* and \? don't work. */
  139. file_enum *
  140. gp_enumerate_files_init(const char *pat, uint patlen,
  141.   proc_alloc_t palloc, proc_free_t pfree)
  142. {    file_enum *pfen = (file_enum *)(*palloc)(1, sizeof(file_enum), "gp_enumerate_files");
  143.     char *pattern;
  144.     char *p;
  145.     int hsize = 0;
  146.     int i;
  147.     if ( pfen == 0 ) return 0;
  148.     pattern = (*palloc)(patlen + 1, 1,
  149.                 "gp_enumerate_files(pattern)");
  150.     if ( pattern == 0 ) return 0;
  151.     p = pattern;
  152.     for ( i = 0; i < patlen; i++ )
  153.        {    switch ( pat[i] )
  154.          {
  155.         case '*':
  156.            /* Skip to . or end of string so DOS can do it. */
  157.            *p++ = '*';
  158.            while ( i < patlen && pat[i] != '.' ) i++;
  159.            i--;
  160.            continue;
  161.         case '\\':
  162.            i++;
  163.            if ( pat[i] != '\\' && pat[i] != '/' && pat[i] != ':' )
  164.             break;
  165.            /* falls through */
  166.         case ':':
  167.         case '/':
  168.            hsize = p + 1 - pattern;
  169.          }
  170.         *p++ = pat[i];
  171.        }
  172.     *p = 0;
  173.     pfen->pattern = pattern;
  174.     pfen->patlen = patlen;
  175.     pfen->head_size = hsize;
  176.     pfen->mprocs.alloc = palloc;
  177.     pfen->mprocs.free = pfree;
  178.     pfen->first_time = 1;
  179.     return pfen;
  180. }
  181.  
  182. /* Enumerate the next file. */
  183. uint
  184. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  185. {    int code;
  186.     char *p, *q;
  187.     if ( pfen->first_time )
  188.        {    code = dos_findfirst(pfen->pattern, &pfen->ffblk);
  189.         pfen->first_time = 0;
  190.        }
  191.     else
  192.         code = dos_findnext(&pfen->ffblk);
  193.     if ( code != 0 )
  194.        {    /* All done, clean up. */
  195.         gp_enumerate_files_close(pfen);
  196.         return ~(uint)0;
  197.        }
  198.     if ( maxlen < 13 + pfen->head_size ) return maxlen + 1;    /* cop out! */
  199.     memcpy(ptr, pfen->pattern, pfen->head_size);
  200.     for ( p = &pfen->ffblk.ff_name[0], q = ptr + pfen->head_size; *p; p++ )
  201.       if ( *p != ' ' ) *q++ = *p;
  202.     return q - ptr;
  203. }
  204.  
  205. /* Clean up the file enumeration. */
  206. void
  207. gp_enumerate_files_close(file_enum *pfen)
  208. {    proc_free_t pfree = pfen->mprocs.free;
  209.     (*pfree)(pfen->pattern, pfen->patlen + 1, 1,
  210.          "gp_enumerate_files_close(pattern)");
  211.     (*pfree)((char *)pfen, 1, sizeof(file_enum), "gp_enumerate_files_close");
  212. }
  213.